<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!--数据表格区域-->
    <el-card>
      <el-row>
        <el-button type="primary" @click="showAddCateDialog()">添加分类</el-button>
      </el-row>
      <el-row>
        <!--表格数据-->
        <zk-table :data="cateList"
                  :columns="columns"
                  :selection-type="false"
                  :expand-type="false"
                  :show-index="true"
                  index-text="##"
                  :show-row-hover="false"
                  border>
          <!--是否有效-->
          <template slot-scope="scope" slot="isok">
            <i class="el-icon-error" style="color: red" v-if="scope.row.cat_deleted"></i>
            <i class="el-icon-success" style="color: lightgreen" v-else></i>
          </template>
          <template slot-scope="scope" slot="order">
            <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag size="mini" v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
            <el-tag size="mini" v-else type="warning">三级</el-tag>
          </template>
          <template slot="opt">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="primary" size="mini" icon="el-icon-delete">删除</el-button>
          </template>

        </zk-table>
        <!--分页-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 50, 100]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-row>
    </el-card>

    <!--添加分类-->
    <el-dialog
      title="添加分类"
      :visible.sync="addCatedialogVisible"
      width="50%"
      @close="addCateFormClose()">
      <el-form
        :model="addCateForm"
        :rules="addCateRules"
        ref="addCateRef"
        label-width="100px"
        class="demo-ruleForm">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!--          options 数据源-->
          <!--          props 用来指定配置对象-->
          <el-cascader
            v-model="selectKeys"
            :options="parentCateList"
            :props="cascderProps"
            @change="parentCateChange()"
            clearable
            filterable>

          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="addCatedialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCate()">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        // 商品分类数据
        queryInfo: {
          type: 3,
          pagenum: 1,
          pagesize: 5
        },
        total: 0,
        cateList: [],
        // 表格渲染
        columns: [{
          label: '分类名称',
          prop: 'cat_name'
        }, {
          label: '是否有效',
          template: 'isok',
          type: 'template' // 当前列为模板
        }, {
          label: '排序',
          template: 'order',
          type: 'template' // 当前列为模板
        }, {
          label: '操作',
          template: 'opt',
          type: 'template' // 当前列为模板
        }],
        addCateForm: {
          cat_pid: 0, // 父级分类
          cat_name: '', // 分类名称
          cat_level: 0 // 当前分类等级
        },
        addCateRules: {
          cat_name: [
            {
              required: true,
              message: '请输入分类名称',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 15,
              message: '长度在 5 到 15 个字符',
              trigger: 'blur'
            }
          ]
        },
        parentCateList: [], // 父级分类列表
        cascderProps: {
          value: 'cat_id',
          label: 'cat_name',
          childred: 'childern',
          checkStrictly: true
        }, // 指定级联选择器的配置对象
        selectKeys: [],
        addCatedialogVisible: false // 分类添加 弹窗
      }
    },
    created () {
      this.getCateList()
    },
    methods: {
      async getCateList () {
        const { data: res } = await this.$http.get('categories', { params: this.queryInfo })
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.cateList = res.data.result
        this.total = res.data.total
      },
      // 监听pagesize改变
      handleSizeChange (newSize) {
        this.queryInfo.pagesize = newSize
        this.getCateList()
      },
      // 监听 每页数量
      handleCurrentChange (newNum) {
        this.queryInfo.pagenum = newNum
        this.getCateList()
      },
      // 点击展示添加分类对话框
      showAddCateDialog () {
        this.getParentCateList()
        this.addCatedialogVisible = true
      },
      // 获取父级分类数据
      async getParentCateList () {
        const { data: res } = await this.$http.get('categories', { params: { type: 2 } })
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.parentCateList = res.data
      },
      // 整理分类
      parentCateChange () {
        if (this.selectKeys.length > 0) {
          // 父级id
          this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
          // 当前id
          this.addCateForm.cat_level = this.selectKeys.length
        } else {
          // 父级id
          this.addCateForm.cat_pid = 0
          // 当前id
          this.addCateForm.cat_level = 0
        }
      },
      // 添加分类
      addCate () {
        console.log(this.addCateForm)
        this.$refs.addCateRef.validate(async valid => {
          if (!valid) return false
          const { data: res } = await this.$http.post('categories', this.addCateForm)
          console.log(res)
          if (res.meta.status !== 201) {
            this.$message.error(res.meta.msg)
          } else {
            this.$message.success(res.meta.msg)
            this.addCatedialogVisible = false
            this.getCateList()
            this.addCateFormClose()
          }
        })
      },
      // 窗口关闭操作
      addCateFormClose () {
        this.$refs.addCateRef.resetFields()
        this.selectKeys = []
        // 父级id
        this.addCateForm.cat_pid = 0
        // 当前id
        this.addCateForm.cat_level = 0
      }
    }
  }
</script>

<style lang="less" scoped>
  .zk-table {
    margin-top: 15px;
  }

  .el-cascader {
    width: 100%;
  }
</style>
